package com.aaa.ssm.servie.impl;

import com.aaa.ssm.entity.Employee;
import com.aaa.ssm.entity.Permission;
import com.aaa.ssm.mapper.EmpMapper;
import com.aaa.ssm.mapper.PermissionMapper;
import com.aaa.ssm.servie.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {


    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    @Async
    public String sendCode(String phone) {
        System.out.println("给："+phone+"发送短信");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "ok";
    }

    @Override
    public Employee login(String username, String password) {

        // 1 根据账号密码 查询员工eid
        Long eid = empMapper.selectEid(username, password);
        if(eid == null){
            throw new RuntimeException("对不起 账号密码错误");
        }
        // 2 根据eid 查询员工信息
        Employee emp = empMapper.getEmpByEid(eid);
        Integer state = emp.getState();
        // 3 黑名单  白名单  员工是否冻结
        if(state.equals(  1  )){
            throw new RuntimeException("账号被冻结");
        } else if (state.equals( 2 )) {
            throw new RuntimeException("员工离职");
        }
        return emp;
    }

    @Override
    public List<Permission> getPermisssion(Integer rid) {
        if(rid == 1){
          return  permissionMapper.getAllPermissions();
        }
        return permissionMapper.getPermissionsByRid(rid);
    }


}
